package com.small.xx.ai.mcp.client.base;

import io.modelcontextprotocol.client.McpClient;
import io.modelcontextprotocol.client.McpSyncClient;
import io.modelcontextprotocol.client.transport.ServerParameters;
import io.modelcontextprotocol.client.transport.StdioClientTransport;
import io.modelcontextprotocol.spec.McpSchema;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * 不行
 */
public class XxAiMcpClientStdioTests {

    public static void main(String[] args) {
        StdioClientTransport clientTransport = new StdioClientTransport(ServerParameters.builder("java").arg("-jar")
            .arg(
                "D:\\java\\workspace\\components\\remote\\xx-ai\\xx-ai-spring\\xx-ai-mcp\\xx-ai-mcp-server-base\\target/xx-ai-mcp-server-base-1.0.0.jar")
            .build());
        McpSyncClient client = McpClient.sync(clientTransport).build();

        client.initialize();
        System.out.println("是否已经初始化:" + client.isInitialized());

        McpSchema.ListToolsResult listToolsResult = client.listTools();
        List<McpSchema.Tool> tools = listToolsResult.tools();
        System.out.println(
            "获取到的tools:" + tools.stream().map(McpSchema.Tool::name).collect(Collectors.joining(",")));
        System.out.println();
        for (McpSchema.Tool tool : tools) {
            McpSchema.JsonSchema jsonSchema = tool.inputSchema();
            System.out.println("工具：==========");
            System.out.println("  " + jsonSchema);
            McpSchema.CallToolResult callToolResult = client.callTool(
                new McpSchema.CallToolRequest(tool.name(), Map.of("city", "北京", "timeZoneId", "Europe/Moscow")));

            System.out.println("获取到的结果为：==========");
            callToolResult.content().forEach(v -> System.out.println("  " + v));
            System.out.println();
        }

    }
}